package yxy.game.pm2.command.plainsarena;

import yxy.cherry.data.DataCenter;
import yxy.cherry.data.bean.packagedata;
import yxy.cherry.game.base.message.MessageEmitterHandler;
import yxy.cherry.game.base.message.annotation.MessageSource;
import yxy.cherry.game.base.message.annotation.MessageType;
import yxy.game.pm2.Result;
import yxy.game.pm2.bean.Currency;
import yxy.game.pm2.bean.PlayerBean;
import yxy.game.pm2.bean.ServerBean;
import yxy.game.pm2.bean.area.AreaBean;
import yxy.game.pm2.bean.currency.Currencies;
import yxy.game.pm2.command.Command;
import yxy.game.pm2.message.PlainsArenaMessage;
import yxy.game.pm2.module.RankWorshipHandle;
import yxy.game.pm2.module.plainsarena.RankPlainsArena;

/** 中原擂台 中原擂台膜拜 */
@MessageType(proto = Command.PlainsArenaRankWorship, classtype = PlainsArenaMessage.PlainsArenaRankWorshipRequest.class)
public class CMD_PlainsArenaRankWorship extends Command<PlainsArenaMessage.PlainsArenaRankWorshipRequest> {

	@Override
	public void execute(MessageSource messageSource, PlainsArenaMessage.PlainsArenaRankWorshipRequest request) {
		String playerCode = messageSource.getTag();
		int ranking = request.getRank();
		int level = request.getLevel();

		PlainsArenaMessage.PlainsArenaRankWorshipResponse.Builder response = PlainsArenaMessage.PlainsArenaRankWorshipResponse.newBuilder();

		if (level < 1 || level > 5) {
			send(messageSource, response, Result.ParamError);
			return;
		}
		PlayerBean playerBean = PlayerBean.get(playerCode);
		ServerBean serverBean = ServerBean.get(playerBean.getServerId());
		AreaBean areaBean = AreaBean.get(serverBean.getAreaId());

		RankPlainsArena rank = new RankPlainsArena(areaBean.getGroupId(), level);
		RankWorshipHandle.RankWorshipEntry worshipEntry = new RankWorshipHandle.RankWorshipEntry();

		packagedata data = DataCenter.getData(3, packagedata.class);
		Currencies rewards = new Currencies(Currency.create(data.getRewardtype(), data.getRewardid(), data.getRewardnum()));

		Result result = new RankWorshipHandle().worship(playerCode, ranking, rank, rewards, worshipEntry);
		if (!result.equals(Result.Success)) {
			send(messageSource, response, result);
			return;
		}
		response.setRank(ranking);
		response.setFans(worshipEntry.getFans());
		response.addAllRewards(worshipEntry.getRewards());
		response.setTimes(worshipEntry.getTimes());

		send(messageSource, response, Result.Success);
	}

	private void send(MessageSource messageSource, PlainsArenaMessage.PlainsArenaRankWorshipResponse.Builder response, Result result) {
		response.setResult(result.packResult());
		MessageEmitterHandler.getInstance().send(messageSource, response.build());
	}
}